using System;
using System.Linq;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class moveTeamShip : FunctionBase
    {
        private MarsExplorerEntities dbContext;
        
        public int TeamID { get; set; }
        public int positionID { get; set; }
        int combustivel = 0, oxigenio = 0, comida = 0, agua = 0, medico = 0, escudo = 0;
        int _combustivel = 0, _oxigenio = 0, _comida = 0, _agua = 0, _medico = 0, _escudo = 0;
        public moveTeamShip(HttpContext context) : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            try
            {
                Team t = (from x in dbContext.Teams where x.TeamID == TeamID select x).FirstOrDefault();
                if (t.canMove == 0 && t.Mining==0)
                {
                    if (checkMoveAllowed())
                    {
                        switch (t.RouteID)
                        {
                            case 1:
                                if (positionID == 2 || positionID == 7 || positionID == 13)
                                {
                                    removeResources();
                                    t.RouteID = positionID;
                                    t.canMove = 1;
                                    dbContext.SaveChanges();
                                    return "OK";
                                }
                                else
                                {
                                    return "Impossivel mover para esta posicao";
                                }

                            case 20:
                                if (positionID == 5 || positionID == 11 || positionID == 18)
                                {
                                    removeResources();
                                    t.RouteID = positionID;
                                    t.canMove = 1;
                                    dbContext.SaveChanges();
                                    return "OK";
                                }
                                else
                                {
                                    return "Impossivel mover para esta posicao";
                                }

                            default:
                                if (t.Route.Prev == positionID || t.Route.Next == positionID || t.Route.SS == positionID)
                                {
                                    removeResources();
                                    t.RouteID = positionID;
                                    t.canMove = 1;
                                    dbContext.SaveChanges();
                                    return "OK";
                                }
                                else
                                {
                                    return "Impossivel mover para esta posicao";
                                }
                        }
                    }
                    else
                    {
                        return "Recursos alocados insuficientes";
                    }
                }
                else
                {
                    return "Ja se moveu neste turno";
                }
            }
            catch (Exception)
            {
                return "ERROR";
            }
        }

        private Boolean checkMoveAllowed()
        {
            getNeededResources();
            getAllocatedResources();

            if (agua >= _agua && combustivel >= _combustivel && oxigenio >= _oxigenio && comida >= _comida && (medico >= _medico || escudo >= _escudo))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private string getNeededResources()
        {
            string teamRoute = (from x in dbContext.Teams where x.TeamID == TeamID select x.Route.RouteType).FirstOrDefault();
            Game g = (from h in dbContext.Games where h.GameID == 1 select h).FirstOrDefault();
            GamesEvent ge = (from z in dbContext.GamesEvents where z.GameID == "1" && z.Route == teamRoute && z.GameTurn==g.RunningTurn select z).FirstOrDefault();


            if (ge != null)
            {
                switch (ge.EventID)
                {
                    case 1:  //explosao solar
                        
                        _agua = 2;
                        _comida = 2;
                        _oxigenio = 2;
                        _combustivel = 2;
                        _medico = 1;
                        _escudo = 1;
                        break;
                    case 2: //meteoritos
                       
                        _agua = 1;
                        _comida = 1;
                        _oxigenio = 3;
                        _combustivel = 3;
                        _medico = 0;
                        _escudo = 0;
                        break;

                }
            }
            else
            {
                _agua = 1;
                _comida = 1;
                _oxigenio = 1;
                _combustivel = 1;
                _medico = 0;
                _escudo = 0;
              
            }
            return "ok";
        }

        private string getAllocatedResources()
        {
            var tt = from tts in dbContext.TeamsTurns where tts.TeamID == TeamID select tts;

            foreach (TeamsTurn j in tt)
            {
                switch (j.ResourceID)
                {
                    case 1:
                        agua = j.Value;
                        break;
                    case 2:
                        combustivel = j.Value;
                        break;
                    case 3:
                        comida = j.Value;
                        break;
                    case 4:
                        oxigenio = j.Value;
                        break;
                    case 5:
                        medico = j.Value;
                        break;
                    case 7:
                        escudo = j.Value;
                        break;
                    default:
                        break;
                }
            }
            return "ok";
        }

        private string removeResources()
        {
            try
            {
                var tr = (from x in dbContext.TeamsResources where x.TeamID == TeamID select x).ToList();
                var tt = (from z in dbContext.TeamsTurns where z.TeamID == TeamID select z).ToList();
                foreach (TeamsTurn f in tt)
                {
                    f.Value = 0;
                }
                foreach (TeamsResource r in tr)
                {
                    switch (r.ResourceID)
                    {
                        case 1:
                            r.Value -= agua;
                            break;
                        case 2:
                            r.Value -= combustivel;
                            break;
                        case 3:
                            r.Value -= comida;
                            break;
                        case 4:
                            r.Value -= oxigenio;
                            break;
                        case 5:
                            r.Value -= medico;
                            break;

                        case 7:
                            r.Value -= escudo;
                            break;
                        default:
                            break;
                    }
                    dbContext.SaveChanges();
                }
                return "ok";
            }
            catch (Exception e )
            {

                return "error";
            }
           
        }

        public override void InitializeProperties()
        {
            try
            {
                TeamID = Convert.ToInt32(Parameters["teamid"]);
                positionID = Convert.ToInt32(Parameters["positionid"]);
                dbContext = new MarsExplorerEntities();
              
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }
}